
function dump()
{
    var s = "";

    for (i in arguments) {
        s += arguments[i];
    }

    s += "<br />";

    document.write(s);
}


//入口 变量 类型
function myvar() {
    var a = "hello world";
    var b;
    /*
     * document.write(typeof "hello");
     * document.write("<br />");
     * document.write(typeof 'hello');
     * document.write("<br />");
     * document.write(typeof 133);
     * document.write("<br />");
     * document.write(typeof 56.378);
     * document.write("<br />");
     * document.write(typeof(0xFF00));
     * document.write("<br />");
     * document.write(typeof null);
     * document.write("<br />");
     */
    document.write(a);
    document.write("<br />");

    var i = parseInt("10");
    document.write(typeof(i) + " 10: " + i + "<br />");
    i = parseInt("10", 2);
    document.write(typeof(i) + " 2: " + i + "<br />");
    i = parseInt("010", 8);
    document.write(typeof(i) + " 8: " + i + "<br />");
    i = parseInt("0xFF", 16);
    document.write(typeof(i) + " 16: " + i + "<br />");

    i = parseFloat("367.8997");
    document.write(typeof(i) + " float: " + i + "<br />");
    document.write(typeof(i) + " float tofixed(2): " + i.toFixed(2) + "<br />");
    document.write("toExponential: " + i.toExponential() + "<br />");
    document.write("toPrecision: " + i.toPrecision(5) + "<br />");

    console.log("输出内容");

    // alert("输出内容");

    //单行注释

    /*
     * 多行批量注释
     *
     * */
}

// myvar();


//函数默认值和可变参功能
function test(a, b, c) {
    // if (typeof(c) === "undefined")
    if (String(c) === "undefined")
        c = 199;

    //访问全局变量
    document.write("function g = " + g + "<br />");

    //如果没有加var，则定义全局变量
    var i = 0;
    for (i in arguments) {
        document.write("arg[" + i + "] = " + arguments[i] + "<br />");
    }

    document.write("a = " + a + ", b = " + b + ", c = " + c + "<br />");
}

//定义全局变量
var g = "hello";

// 函数自调用
var r = (function(){return "function kyo"})();

// test(10, 100, 444, 555, 666, 777);
// document.write("i = " + i);
// document.write("r = " + r);

// document.write("hello".length);


function function_test()
{
    var i = 0;

   return function(){ return ++i; };
}

result = function_test();

document.write("function_test: " + result() + "<br />");
document.write("function_test: " + result() + "<br />");
document.write("function_test: " + result() + "<br />");
document.write("function_test: " + result() + "<br />");
document.write("function_test: " + result() + "<br />");


function string_test()
{
    dump("============ string ===================");
    var s = "hello world 0";

    dump("s: ", s);
    dump("s[6]: ", s[6]);
    dump("s[6] code : ", s.charCodeAt(6));
    dump("length: ", s.length);
    dump("indexOf l: ", s.indexOf('l'));
    dump("indexOf z: ", s.indexOf('z'));
    //从第五个字符开始查询
    dump("indexOf l 5: ", s.indexOf('l', 5));
    var r = new RegExp(/[0-9]/);
    dump("regexp match: ", s.match(r));
}

// string_test();

function object_test()
{
    dump("============ object ===================");
    var i;

    // 对象定义
    /*
     * var obj = {
     *     "name": "kyo",
     *     "sex": true,
     *     "age": 16,
     *     "run": function() {
     *         dump("obj run method!");
     *     },
     * };
     */

    //构造函数
    function stu(name, sex, age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.run = function() {
            dump("obj run method!");
            dump("run obj.name = ", this.name);
        }
    }

    var obj = new stu("kyo", true, 23);

    //对象的属性赋值
    obj.name = "tom";
    obj["age"] = 23;

    //属性的删除
    delete obj.sex;

    //判断属性是否存在
    if ("sex" in obj)
        dump("sex in obj!");

    // dump("obj.name = ", obj.name);
    // dump("obj.age = ", obj.age);
    //遍历对象
    for (i in obj) {
        dump(i, ":", obj[i]);
    }

    // delete obj.run;
    if ("run" in obj)
        obj.run();

    var s = JSON.stringify(obj);
    dump("JSON: ", s);
    var nObj = JSON.parse(s);
    for (i in nObj) {
        dump(i, ":", nObj[i]);
    }
    nObj.run();
}

// object_test();
// this.object_test();
// object_test.call();

function array_test()
{
    dump("============ array ===================");

    var a = [133, 345.45, "hello", true, {}];

    dump("a: ", a);
    a[3] = false;
    dump("a[3]: ", a[3]);
    dump("array length: ", a.length);

    a.push(7888);
    dump("push a: ", a);
    dump("pop: ", a.pop());
    dump("pop a: ", a);
    // dump("a tostring: ", a.toString());
}
array_test();

function error_test()
{
    try {
        if (a > 10)
            dump("error");
        dump("正常代码执行中....");
    } catch (e) {
        dump("error message: ", e.message);
    } finally {
        dump("总会执行的代码!");
    }
}

error_test();


